package com.wushi.lenovo.asia5b.app_ui.voucher.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mvp.images.WImageLoader;
import com.mvp.utils.ResString;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.voucher.VoucherModel;
import com.wushi.lenovo.asia5b.app_model.voucher.VoucherType;
import com.wushi.lenovo.asia5b.app_widget.TiltView;

import java.util.List;

/**
 * Created by zhangyuncai on 2019/1/7.
 */
public class VoucherAdapter extends BaseQuickAdapter<VoucherModel, BaseViewHolder> {

    final VoucherType voucherType;
    final int colorGray;
    final int colorBlack;
    final int colorBlue;
    final String textUsed;
    final String textExpired;

    public VoucherAdapter(@Nullable List<VoucherModel> data, VoucherType voucherType, Context context) {
        super(R.layout.item_voucher_adapter, data);
        this.voucherType = voucherType;
        colorGray = ContextCompat.getColor(context, R.color.common_text_gray);
        colorBlack = ContextCompat.getColor(context, R.color.common_text_black);
        colorBlue = ContextCompat.getColor(context, R.color.colorPrimaryDark);
        textExpired = context.getString(R.string.voucher_expired).toUpperCase();
        textUsed = context.getString(R.string.voucher_used).toUpperCase();
    }

    @Override
    protected void convert(BaseViewHolder helper, VoucherModel item) {
        int position = helper.getLayoutPosition();

        //图片 iv_voucher_img
        ImageView imageView = helper.getView(R.id.iv_voucher_img);
        WImageLoader.displayImage(mContext, item.getImg_url(), imageView);

        //名称 tv_voucher_name
        helper.setText(R.id.tv_voucher_name, item.getVoucher_name());

        //描述 tv_voucher_desc
        helper.setText(R.id.tv_voucher_desc, item.getDescription());

        String code = String.format(ResString.get(R.string.voucher_code), item.getVoucher_code());
        //code tv_voucher_code
        helper.setText(R.id.tv_voucher_code, code);

        //有效期 tv_voucher_time  2019-02-01 00:00:00  转换 02-01-2019
//        String time=  WTimeUtils.switchDateFormatter(item.getExpired_date(),WTimeUtils.text_yyyy_MM_dd_HH_mm_ss,WTimeUtils.text_dd_MM_yyyy);
        helper.setText(R.id.tv_voucher_time, String.format(ResString.get(R.string.valid_till),item.getExpired_date()));


        //分割线 view_voucher_topline  view_voucher_bottomline
        helper.setGone(R.id.view_voucher_bottomline, position == (mData.size() - 1));

        //已使用标志
        TiltView tiltView = helper.getView(R.id.tiltview);

        if (voucherType == VoucherType.unused) {//未使用
            tiltView.setVisibility(View.GONE);
            helper.setTextColor(R.id.tv_voucher_name, colorBlue);
            helper.setTextColor(R.id.tv_voucher_desc, colorBlack);
            helper.setTextColor(R.id.tv_voucher_code, colorBlack);
            helper.setTextColor(R.id.tv_voucher_time, colorGray);
        } else if (voucherType == VoucherType.used) {//已使用
            tiltView.setVisibility(View.VISIBLE);
            tiltView.setText(textUsed);
            helper.setTextColor(R.id.tv_voucher_name, colorGray);
            helper.setTextColor(R.id.tv_voucher_desc, colorGray);
            helper.setTextColor(R.id.tv_voucher_code, colorGray);
            helper.setTextColor(R.id.tv_voucher_time, colorGray);
        } else {//已过期
            tiltView.setVisibility(View.VISIBLE);
            tiltView.setText(textExpired);
            helper.setTextColor(R.id.tv_voucher_name, colorGray);
            helper.setTextColor(R.id.tv_voucher_desc, colorGray);
            helper.setTextColor(R.id.tv_voucher_code, colorGray);
            helper.setTextColor(R.id.tv_voucher_time, colorGray);
        }

    }
}
